import React from 'react';
import { Switch, Route, Redirect } from 'dva/router';

function RouterView(props:any) {
  const { routes } = props;
  if (!routes || routes.length === 0) { return null; }
  return (
    <Switch>
      {routes.map((item:any, index:number) => {
        const { path } = item;
        return (
          <Route
            key={item.path}
            path={path}
            exact={item.exact}
            render={(renderProps) => {
              const Component:any = item.component;
              if (!Component) { return null; }

              return <Component {...renderProps} {...item} />;
            }}
          />
        );
      })}
      <Redirect to="/404" />
    </Switch>
  );
}
RouterView.defaultProps = {
  routes: [],
};

export default RouterView;
